package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.AllMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private AllMapper allMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO getBusinessData() {
       /* List<LocalDate> dateList = new ArrayList<>();
        dateList.add(LocalDate.now());
        List<ReportVO> reportVOS = orderMapper.orderCountList(dateList);

        Long validOrderCount = 0L;
        if (reportVOS != null && reportVOS.size() != 0) {
            validOrderCount = reportVOS.get(0).getTurnover();
        }

        Integer orderCoun = orderMapper.validOrderCount(dateList);
        if (orderCoun == null) {
            orderCoun = 0;
        }

        List<ReportVO> reportVOS1 = orderMapper.seletUser(dateList);
        Long newUsers = 0L;
        if (reportVOS1 != null && reportVOS1.size() != 0) {
            newUsers = reportVOS1.get(0).getTurnover();
        }

        // Long turnover = orderMapper.turnoverStatistics(dateList).get(0).getTurnover();
        List<ReportVO> reportV = orderMapper.turnoverStatistics(dateList);
        Long turnover = 0L;
        if (reportV != null && reportV.size() != 0) {
            newUsers = reportV.get(0).getTurnover();
        }
        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;
        if (validOrderCount != 0) {
            unitPrice = (double)(turnover / validOrderCount);
            orderCompletionRate = (double) validOrderCount / orderCoun;
        }

        return BusinessDataVO.builder()
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUsers.intValue())
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount.intValue())
                .turnover(turnover.doubleValue()).build();*/


        return allMapper.findAll(LocalDate.now());

    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setmealMapper.selectSetmealsByStatus(1);
        Integer discontinued = setmealMapper.selectSetmealsByStatus(0);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued).build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.selectDishByStatus(1);
        Integer discontinued = dishMapper.selectDishByStatus(0);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued).build();
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {

        Integer waitingOrders = orderMapper.findTodayOrders(Orders.TO_BE_CONFIRMED);
        Integer deliveredOrders = orderMapper.findTodayOrders(Orders.CONFIRMED);
        Integer completedOrders = orderMapper.findTodayOrders(Orders.COMPLETED);
        Integer cancelledOrders = orderMapper.findTodayOrders(Orders.CANCELLED);
        Integer allOrders = orderMapper.findTodayOrders(null);


        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders).build();
    }
}
